﻿using Common.Attributes;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace QGZX.Logic.Cwy
{
    public class WorkingController :　Controller
    {
        #region 加载乘务员志愿者名单信息的视图--ActionResult Index()
        /// <summary>
        /// 加载乘务员志愿者名单信息的视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region 加载乘务员志愿者名单信息的视图 +GetVolunteerData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 加载乘务员志愿者名单信息的视图
        /// </summary>
        /// <returns></returns>
        public ActionResult GetVolunteerData()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            var list = OperateContext.Current.BLLSession.Iqgzx_cwy_volunteerBLL.GetPagedList(pageIndex, pageSize, x => x.VolunteerId != null, m => m.StudentNum).Select(x => x.ToPOCO());
            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 加载修改乘务员志愿者名单信息表的 window -- ActionResult EditVolunteer（）
        /// <summary>
        /// 加载修改乘务员志愿者名单信息表的 window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditVolunteer(int id)
        {
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_cwy_volunteerBLL.GetListBy(x => x.VolunteerId == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改乘务员报名信息表的 window EditVolunteer(MODEL.qgzx_cyw_volunteer model)
        /// <summary>
        ///post加载修改乘务员志愿者名单信息表的 EditVolunteer(MODEL.qgzx_cyw_volunteer model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditVolunteer(MODEL.qgzx_cwy_volunteer model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_cwy_volunteerBLL.Modify(model, "IsHeadman", "ToGroup", "WorkDays", "AbsentDays", "ReplaceDays", "RunningDays", "VocationDays", "OrdinaryVocation", "SpareLost", "SparePay");
            if (res > 0)
            {
                return Redirect("/cwy/Working/Index");
            }
            return Redirect("/cwy/Working/Index");
        }
        #endregion

        #region 删除乘务员志愿者信息 + DelVolunteer()
        /// <summary>
        /// 删除乘务员志愿者信息 + DelVolunteer()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelVolunteer()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_cwy_volunteerBLL.DelBy(x => x.VolunteerId == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "出错了,您现在删除的学生正在操作" + ex, null, "");
            }
        }
        #endregion

        #region 查找乘务员志愿者的详细信息--ActionResult PersonalDetail(int id)
        /// <summary>
        /// 查找乘务员志愿者的详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult PersonalDetail(int id)
        {
            string StudentNum = OperateContext.Current.BLLSession.Iqgzx_cwy_volunteerBLL.GetListBy(x => x.VolunteerId == id).FirstOrDefault().StudentNum;
            var model = OperateContext.Current.BLLSession.Iqgzx_cwy_enrollBLL.GetListBy(x => x.StudentNum == StudentNum).FirstOrDefault().ToViewModel();
            var list = OperateContext.Current.BLLSession.Iqgzx_student_informationBLL.GetListBy(x => x.StudentNum == model.StudentNum).FirstOrDefault().ToViewModel();
            if (list != null)
            {
                int Gender = list.Gender;
                int Grade = (int)list.Grade;
                int Height = (int)list.Height;
                string Dormitory = list.Dormitory;
                int PoliticStatus = (int)list.PoliticStatus;
                string College = OperateContext.Current.BLLSession.Iqgzx_collegeBLL.GetListBy(x => x.CollegeId == list.College).FirstOrDefault().CollegeName;
                string Profession = OperateContext.Current.BLLSession.Iqgzx_professionBLL.GetListBy(x => x.ProfessionId == list.Profession).FirstOrDefault().ProfessionName;
                if (Gender == 0)
                {
                    ViewBag.Gender = "男";
                }
                else
                {
                    ViewBag.Gender = "女";
                }
                if (PoliticStatus == 0) { ViewBag.PoliticStatus = "中共党员"; }
                if (PoliticStatus == 1) { ViewBag.PoliticStatus = "预备党员"; }
                if (PoliticStatus == 2) { ViewBag.PoliticStatus = "共青团员"; }
                if (PoliticStatus == 3) { ViewBag.PoliticStatus = "群众"; }
                else { ViewBag.PoliticStatus = "其他党派"; }
                ViewBag.Height = Height;
                ViewBag.Dormitory = Dormitory;
                ViewBag.Grade = Grade;
                ViewBag.College = College;
                ViewBag.Profession = Profession;
            }
            return PartialView(model);
        }
        #endregion

        #region 最终审核，确认志愿者名单 window -- ActionResult FinalAudition（）
        /// <summary>
        ///  最终审核，确认志愿者名单  window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult FinalAudition(int id)
        {
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_cwy_volunteerBLL.GetListBy(x => x.VolunteerId == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post 最终审核，确认志愿者名单信息  window FinalAudition(MODEL.qgzx_cyw_volunteer model)
        /// <summary>
        ///post 最终审核，确认志愿者名单信息  FinalAudition(MODEL.qgzx_cyw_volunteer model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult FinalAudition(MODEL.qgzx_cwy_volunteer model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_cwy_volunteerBLL.Modify(model, "IsHeadman", "ToGroup", "WorkDays", "AbsentDays", "ReplaceDays", "RunningDays", "VocationDays", "OrdinaryVocation", "SpareLost", "SparePay", "FinalWage", "Deeds", "Punishment", "IsExcellent");
            if (res > 0)
            {
                return Redirect("/cwy/Working/Index");
            }
            return Redirect("/cwy/Working/Index");
        }
        #endregion

        #region 搜索方法 --ActionResult Search()
        /// <summary>
        /// 多条件搜索
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {
            string StudentNum = Request.QueryString["StudentNum"];
            string StudentName = Request.QueryString["StudentName"];
            string Deeds = Request.QueryString["Deeds"];
            string Punishment = Request.QueryString["Punishment"];

            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！

            var list = OperateContext.Current.BLLSession.Iqgzx_cwy_volunteerBLL.GetListBy(x => x.VolunteerId != null).ToList().Select(x => x.ToPOCO());
            if (StudentNum != "")
            {
                list = list.Where(x => x.StudentNum.Contains(StudentNum)).ToList();
            }
            if (StudentName != "")
            {
                list = list.Where(x => x.StudentName.Contains(StudentName)).ToList();
            }
            if (Deeds != null)
            {
                list = list.Where(x => x.Deeds.Contains(Deeds)).ToList();
            }
            if (Punishment != "")
            {
                list = list.Where(x => x.Punishment.Contains(Punishment)).ToList();
            }
            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion
    }
}
